package cn.tedu;


import org.junit.Test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class JdbcDome2 {
    /*新增表记录:往account表中添加一条记录:null 'helln' 3500*/
    @Test
    public void testAdd() throws Exception{
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3307/jt_db?characterEncoding=utf-8&serverTimezone=Asia/Shanghai", "root", "root");
        Statement stat = connection.createStatement();
        String sql="insert into account value(null,'hellen',3500)";
        int rows = stat.executeUpdate(sql);
        System.out.println("影响的行数是:"+rows);
        stat.close();
        connection.close();
    }
    @Test
    public void testUpdate() throws Exception {
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3307/jt_db?characterEncodig=utf-8&serverTimezone=Asia/Shanghai", "root", "root");
        Statement stat = connection.createStatement();
        int rows = stat.executeUpdate("update account set money=5000 where name='hellen'");
        System.out.println("影响的行数是:"+rows);
        stat.close();
        connection.close();


    }
    @Test
    public void testDelete(){
        Statement stat = null;
        Connection connection = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3307/jt_db?characterEncodig=utf-8&serverTimezone=Asia/Shanghai", "root", "root");
            stat = connection.createStatement();
            int rows = stat.executeUpdate("delete from account where name='hellen'");
            System.out.println("影响的行数是:"+rows);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                stat.close();
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
}
